/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-09-03 01:20:43
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-26 15:04:43
 * @FilePath: \keep_motion_api\controllers\sport\sportActivities.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Activity } = require('@/models/sport');
const { clearObject } = require('@/utils');

// 查询运动活动
const getSportActivities = async (req, res) => {
  try {
    const { title, type, start_date, end_date, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Activity.getSportActivities(title, type, start_date, end_date, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询运动活动失败', error });
  }
}
const createSportActivities = async (req, res) => { 
  try {
    const body = req.body;
    const params = clearObject(body);
    const sportActivities = await Activity.createSportActivities(params);
    res.status(200).send(sportActivities);
  } catch (error) {
    res.status(500).json({ message: '创建运动活动失败', error });
  }
}
const updateSportActivities = async (req, res) => {
  try {
    const { id } = req.params;
    const body = req.body;
    const params = clearObject(body);
    const sportActivities = await Activity.updateSportActivities(id, params);
    res.status(200).send(sportActivities);
  } catch (error) {
    res.status(500).json({ message: '更新运动活动失败', error });
  }
}
const getActivityParticipants = async (req, res) => {
  try {
    const { id } = req.query;
    const sportActivities = await Activity.getActivityParticipants(id);
    res.status(200).send(sportActivities);  
  } catch (error) {
    res.status(500).json({ message: '查询运动活动参与人失败', error });
  }
}
module.exports = {
  getSportActivities,
  createSportActivities,
  updateSportActivities,
  getActivityParticipants
}